package cn.mifengkong.appcommon.webview.jshandler;

import com.mifengkong.frtools.jsbridge.CallBackFunction;
import com.mifengkong.frtools.jsbridge.JsHandler;
import com.mifengkong.frtools.util.FRString;

import org.json.JSONException;
import org.json.JSONObject;

import cn.mifengkong.appcommon.webview.listener.JsBridgeCallBackListener;

/**
 * Created by jack on 2018/3/6
 */

public class AliPayJsHandler implements JsHandler {

    private String orderInfo;
    private JsBridgeCallBackListener mListener;

    public AliPayJsHandler(JsBridgeCallBackListener listener) {
        mListener = listener;
    }

    @Override
    public void OnHandler(String handlerName, String responseData, CallBackFunction function) {
        if (null != mListener && parseResponseData(responseData)) {
            mListener.aliPay(orderInfo);
        }
    }

    /**
     * 是否需要跳转支付页面
     */
    private boolean parseResponseData(String responseData) {
        try {
            JSONObject responseDataJO = new JSONObject(responseData);
            orderInfo = responseDataJO.getString("order_info");
            return !FRString.isEmpty(orderInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
}
